package cn.tedu.csmall.stock.webapi.service.impl;

import cn.tedu.csmall.commons.exception.CoolSharkServiceException;
import cn.tedu.csmall.commons.pojo.stock.dto.StockReduceCountDTO;
import cn.tedu.csmall.commons.restful.ResponseCode;

import cn.tedu.csmall.stock.service.IStockService;
import cn.tedu.csmall.stock.webapi.mapper.StockMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * IStockService的业务实现类,
 */
@DubboService
@Service
@Slf4j
public class StockServiceImpl implements IStockService {

    @Autowired
    private StockMapper stockMapper;

    /**
     * 修改库存信息的业务实现方法
     *
     * @param stockReduceCountDTO
     */
    @Override
    public void updateStockCount(StockReduceCountDTO stockReduceCountDTO) {
        int rows = stockMapper.updateStockCount(stockReduceCountDTO.getReduceCount(), stockReduceCountDTO.getCommodityCode());
        //如果数据库返回的受影响行数=0表示没有受影响的记录信息，此时表示没有可减少的库存，要抛出异常处理
        if (rows == 0) {
            String message = "此商品库存信息已经不足，请耐心等待补货信息~";
            throw new CoolSharkServiceException(ResponseCode.BAD_REQUEST, message);
        }
        log.info("库存减少完成!!");
    }

}
